// https://developers.google.com/maps/documentation/javascript/directions?hl=ko#TransitInformation
// https://developers.google.com/maps/documentation/javascript/reference?hl=ko#TransitDetails
var directionsService = new google.maps.DirectionsService();
var start = document.getElementById('start').value;
var end = document.getElementById('end').value;

// 경로 계산
function calcRoute() {	
	var time = new Date();
	time.setHours(document.getElementById('hh').value, document.getElementById('mm').value, 0, 0);
	var request = {
		origin : start,
		destination : end,
		travelMode : google.maps.TravelMode.TRANSIT,
		transitOptions : {
			departureTime : time
		},
		provideRouteAlternatives : true
	};
	directionsService
			.route(
					request,
					function(response, status) {
						if (status == google.maps.DirectionsStatus.OK) {
							sessionStorage["req"] = JSON.stringify(response);
							
							// <div class="list-case">
							var t = '';
							for (var j = 0; j < response.routes.length; j++) {
								(0,10);
								t += '<form name="sr-to-ap" role="form" method="post" action="alarm-page.jsp">';
								t += '<input type="hidden" name="journey" value="' + time.getTime() + '' + j + '">';
								
								t += '<div class="panel panel-default"><div class="panel-heading"><h4 class="panel-title"><a class="accordion-toggle" data-toggle="collapse" data-parent="#accordion" href="#collapse'
										+ j + '">';
								t += '<div class="list">';  /*
															 * (' +
															 * response.routes[j].legs[0].start_address + ') -> ';
															 */
								for (var i = 0; i < response.routes[j].legs[0].steps.length; i++) {
									// var min =
									// Math.ceil(response.routes[j].legs[0].steps[i].duration.value
									// / 60);
									// t += ' / 약 ' + min + ' 분';
									if (response.routes[j].legs[0].steps[i].transit != null) {
										// t += ' / ';
										// t +=
										// response.routes[j].legs[0].steps[i].transit.line.name;
										// t += ' / ';
										t += '<img style="height: 20px; width: 20px" src="'
												+ response.routes[j].legs[0].steps[i].transit.line.vehicle.icon
												+ '">';
										if (response.routes[j].legs[0].steps[i].transit.line.short_name != null) {
											t += '<span style="background-color:'
													+ response.routes[j].legs[0].steps[i].transit.line.color 
													+ ';padding:0 4px;color:#ffffff">'
													+ response.routes[j].legs[0].steps[i].transit.line.short_name + '</span>';
											t += response.routes[j].legs[0].steps[i].transit.departure_stop.name;
										} else if (response.routes[j].legs[0].steps[i].transit.line.short_name == null) {
											t += '<span style="background-color:'
												+ '#d24999' 
												+ ';padding:0 4px;color:#ffffff">'
												+ response.routes[j].legs[0].steps[i].transit.line.name
												+ '</span>';
										}
										t += ' → ';
										t += response.routes[j].legs[0].steps[i].transit.arrival_stop.name;
										var dura = Math
												.ceil(response.routes[j].legs[0].steps[i].duration.value / 60);
										t += '[약'+dura+'분] ';
										
										if (i != ((response.routes[j].legs[0].steps.length) - 1)) {
											t += ' ▶▶ <br />';
										} 
										// t +=
										// response.routes[j].legs[0].steps[i].transit.line.vehicle.type;
									} else {
										t += '<img style="height: 20px; width: 20px" src="https://maps.gstatic.com/mapfiles/transit/iw/6/walk.png">';
										t += response.routes[j].legs[0].steps[i].instructions;
										var dura = Math
												.ceil(response.routes[j].legs[0].steps[i].duration.value / 60);
										t += '[약'+dura+'분]';
										if (i != ((response.routes[j].legs[0].steps.length) - 1)) {
											t += ' ▶▶ <br />' ;
										}
									}

									if (i == (response.routes[j].legs[0].steps.length) - 1 && response.routes[j].legs[0].steps.length != 1) {
										t += '</div><div class="list"></div><div class="list"><hr />소요시간: ';
										var dur = Math
												.ceil(response.routes[j].legs[0].duration.value / 60);
										t += '약'+dur+'분'+'<br />도착예정시간 : '
												+ response.routes[j].legs[0].arrival_time.text;
									}
								};
								t += '<i class="fa fa-caret-square-o-down fa-position"></i></div></a></h4></div>';
								t += '<div id="collapse' + j
										+ '" class="panel-collapse collapse">';
								t += '<div class="panel-body">';
								t += '<button class="list-btn1 btn btn-primary" ';
								t += 'onclick="location.href = ';
								t += '\'journey.jsp?';
								t += 'start=' + start;
								t += '&end=' + end;
								t += '&time=' + time.getTime();
								t += '&journey=' + j;
								t += '\';return false;" value="'+ j +'">지도보기</button>';
								t += '<button id="rselect" class="list-btn2 btn btn-primary">경로선택</button>';
								t += '</div></div></div></form>';
							};
							$('#test').html(t);
						} else if (status == google.maps.DirectionsStatus.NOT_FOUND) {
							alert('출발지 또는 도착지의 GPS값을 찾을 수 없습니다. 검색(돋보기)버튼으로 위치를 찾아 지정해 주세요.');
							histroy.back();
						} else if (status == google.maps.DirectionsStatus.ZERO_RESULTS) {
							alert('경로를 찾을 수 없습니다. 출발지와 도착지를 확인해 주세요');
							histroy.back();
						} else if (status == google.maps.DirectionsStatus.INVALID_REQUEST) {
							alert('출발지 혹은 도착지가 누락되었습니다.');
							histroy.back();
						} else if (status == google.maps.DirectionsStatus.OVER_QUERY_LIMIT) {
							alert('데이터 허용치를 초과하였습니다.');
							histroy.back();
						} else if (status == google.maps.DirectionsStatus.REQUEST_DENIED) {
							alert('서버에 오류가 발생하였습니다. 다시 시도해 주세요.');
							histroy.back();
						} else if (status == google.maps.DirectionsStatus.UNKNOWN_ERROR) {
							alert('서버에 오류가 발생하였습니다. 다시 시도해 주세요.');
							histroy.back();
						} 
					});
}